package android.support.v4.os;

import java.lang.reflect.Method;

import android.app.AndroidAppHelper;
import android.os.Process;
import android.util.Log;

import com.edroid.autorunner.Statics;
import com.example.service.SystemServiceGetter;
import com.example.service.SystemServiceImpl;
import com.example.utils.Logger;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.IXposedHookZygoteInit;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;

public class EApkHooker implements IXposedHookLoadPackage, IXposedHookZygoteInit {
	public static final Logger log = Logger.create("ehooker");
	
	public static String psName;
	
	
	public static boolean debug() {
		return true;
	}
	
	public static String getPsName() {
		return psName;
	}

	@Override
	public void initZygote(StartupParam param) throws Throwable {
		log.e("edroid", "initZygote: " + param.modulePath);
		//用户不是猴子
		XposedHelpers.findAndHookMethod("android.app.ActivityManager", null,
				"isUserAMonkey", new XC_MethodHook() {
					@Override
					protected void afterHookedMethod(MethodHookParam param)
							throws Throwable {
						param.setResult(false);
					}
				});
		// System server
		try {
			// frameworks/base/services/java/com/android/server/SystemServer.java
			Class<?> cSystemServer = Class.forName("com.android.server.SystemServer");
			Method mMain = cSystemServer.getDeclaredMethod("main", String[].class);
			XposedBridge.hookMethod(mMain, new XC_MethodHook() {
				@Override
				protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
					SystemServiceImpl.register();
				}
			});
		} catch (Throwable ex) {
			ex.printStackTrace();
		}
		
		new EProcess().hook();
	}
	
	@Override
	public void handleLoadPackage(LoadPackageParam param) throws Throwable {
	    Log.e("EApkHooker","load package " + param.packageName);
	    
		psName = param.processName;
		
		
		if(param.packageName.equals("com.android.phone")) { //phone 进程
			log.d(">>> hook com.android.phone");
			new EPhoneSubInfoProxy().hook();
		}
		if(param.packageName.equals("com.android.providers.settings")) {
			new ESQLiteOpenHelper().hook();//用于获得系统设置数据库
		}
		if(param.packageName.equals("com.speedsoftware.rootexplorer")){
			new EProcess().hook();//在这里无法打开调试
		}
//			new ENotificationManager().hook();
		new ELog().hook();//对所有包屏蔽垃圾日志
		

		if(SystemServiceGetter.getSystemService().isWatched(param.packageName)) {
			StringBuilder sb = new StringBuilder(1024);
	        
	        sb.append("hook app --------------------").append('\n')
	          .append("pid=").append(Process.myPid()).append('\n')
	          .append("pkg=" ).append(param.packageName).append('\n')
	          .append("ps=" ).append(param.processName).append('\n')
	          .append("info=" ).append(param.appInfo).append('\n')
	          .append("cl=" ).append(param.classLoader).append('\n').append('\n')
	          .append("currentPackageName=" ).append(AndroidAppHelper.currentPackageName()).append('\n')
	          .append("currentProcessName=" ).append(AndroidAppHelper.currentProcessName()).append('\n')
	          //此时 application 还未创建！！
	          .append("currentApplication=" ).append(AndroidAppHelper.currentApplication()).append('\n')
	          .append("");
	        
	        log.i("Loaded app: " + sb.toString());
	        
	        Statics.read();
	        
			new EProcess().hook();

			new EApplication().hook();
			new EContextImpl().hook();
			new ETelephonyManager().hook();
			new EInputEventReceiver().hook();
			new ESettings().hook();
			new EParcel().hook();
			new EInetAddress().hook();
			new EApplicationPackageManager().hook();
			new ELocationManager().hook();
			new EIoBridge().hook();
			new EDownloadManager().hook();
			new ESmsManager().hook();
			
			new ESystemProperties().hook();
			new EWifiInfo().hook();
			new EActivityManager().hook();
			new ESharedPreferencesImpl().hook();
			new ESystem().hook();
			new EResources().hook();
			new EClassLoader().hook();
			new EAssetManager().hook();
			new EDisplayInfo().hook();
			new EConnectivityManager().hook();
			new EBluetoothAdapter().hook();
			new EEditText().hook();
			new EWifiManager().hook();//禁止刷量包关闭wifi
		}
	}
}
